use lib "../BuildTool/";
use BuildTool;
use Cwd;
use Data::Dumper;

use Test::More tests => 7;
use Test::Exception;

BEGIN {

 	use_ok(VCS);
 	use_ok(BuildTool);
 }
 
my $username; 
 
subtest 'Test Case 2.1' => sub {
	plan tests => 3;
####################################################################################
#	
#       Verifying first build detection and checkout
#
####################################################################################
	#Verifying First Build
	my $path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS2/TC2_1/";
	$username = "desoua1";
	#Checkout TC2_1 with no rel_h.txt - first build
	VCS->checkOutRelHis($path,$username);
	is(BuildTool->checkIsFirstBuild("TC2_1/rel_h.txt"),"true", "verifying that is the first build");
	system("rm -rf TC2_1");
	#Verifying Successful Checkout for First Build
	VCS->coFirstBuild("http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/");
	my $fileExists = "false";
	#Check if the trunk folder exists, indicating that the checkout was successful
	if (-d "trunk") {
		$fileExists = "true";	
	}
	is($fileExists,"true","verifying successful checkout if it's the first build");
	system("rm -rf trunk");	 
	#Verifying Not First Build - path with the rel_h.txt file
	my $path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/";
	VCS->checkOutRelHis($path,$username);
	is(BuildTool->checkIsFirstBuild("tags/release_history.txt"),"false", "verifying if it\'s NOT the first build");
	system("rm -rf tags");

};

subtest 'Test Case 2.2' => sub {
	plan tests => 3;
####################################################################################
#	
#       Verifying first baseline and not the first build detection
#
####################################################################################	
	my $path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS2/TC2_2/";
	my $type = "baseline";
	#Get release_history.txt
	VCS->checkOutRelHis($path,$username);
	#Check it's not the first build
	is(BuildTool->checkIsFirstBuild("TC2_2/rel_h.txt"),"false", "verifying that is not the first build because there\'re crons");
	#Get the previous type (in this case, null) and the first_base flag
	my @prev_type_first_build = BuildTool->getPrevBuildName("TC2_2/rel_h.txt",$type);
	$prev_type = $prev_type_first_build[0];
	$first_base = $prev_type_first_build[1];
	#Check if is the first baseline
	is($prev_type,"", "verifying that the previous build is null");
	is($first_base,"true", "verifying that it's the first baseline");
	system("rm -rf TC2_2");	
};

subtest 'Test Case 2.3' => sub {
	plan tests => 3;
####################################################################################
#	
#       Verifying that buildInfo was successfully created for the FIRST BUILD and FIRST CRON
#
####################################################################################
	my $buildParams = "buildParams.txt";
 	my $params_ref = BuildTool->readParams($buildParams);
 	#Initializing the necessary variables
 	my @params = @$params_ref;
 	my $code_dir = $params[2];
 	my $vcs_path = $params[1];
 	my $first_build = "true";
 	my $first_cron = "true";
 	my $fileExists = "false";
 	#Checkout trunk_test with the files
	VCS->coFirstBuild("http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS2/TC2_3/trunk_test/");
	#Generate buildInfo.txt
	BuildTool->genBuildInfo($code_dir,"trunk_test",$first_build,$first_cron,"buildInfo.txt",$vcs_path);
	#Check if the buildInfo file exists
	if(-e "trunk_test/quals/main_fw/code/build/buildInfo.txt"){
		$fileExists = "true";
	}
	
	is($fileExists,"true", 'Verifying if buildInfo was created in the right folder');
	open(FILE,"trunk_test/quals/main_fw/code/build/buildInfo.txt");
	#Read buildInfo file
	while( my $line = <FILE>) {
		chomp($line);
		push @buildInfo_params, $line; 
	}
	#Check if the files are equal to expected
	if ($buildInfo_params[0] =~ "http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk_test/quals/main_fw/code/test1.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk_test/quals/main_fw/code/test2.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk_test/quals/main_fw/code/test1.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk_test/quals/main_fw/code/build/config.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk_test/quals/main_fw/code/build/buildInfo.txt ", 'Verifying files from buildInfo.txt')
	{
		$files_equal = "true";
	}
	#Get the buildTool_tests path to enter the trunk_test and then comeback
	$build_tool_dir = getcwd();
	chdir("trunk_test");
	#Get the svn file version to compare with the revision in the buildInfo.txt
	open(CMD,"svnversion|");
	$svn_version = <CMD>;
	chomp($svn_version);
	close CMD;
	chdir($build_tool_dir);
	if ($buildInfo_params[1] =~ "$svn_version $svn_version $svn_version $svn_version"){
		$rev_equal = "true";
	}
	
	is($files_equal, "true","Verifying files from buildInfo.txt");
	is($rev_equal, "true", "Verifying revision parameter from buildInfo.txt");
	system ("rm -rf trunk_test");
	
};

subtest 'Test Case 2.4' => sub {
	plan tests => 6;
#################################################################################################
#	
#       Verifying that buildInfo was successfully created for the FIRST BUILD and FIRST BASELINE
#
#################################################################################################
	my $its_path = "http://cad-test", my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool";
	my $project_name = "playground";
	my $rel_inc = "inc2";
	my $type = "baseline";
	my $base_open_wi_allw = "false";
	my $buildReport = "buildReport";
	my $first_base = "true", $first_build = "true", $first_cron = "false";
	my $type = "baseline";
	my $base_open_wi_allw = "true";
	$rel_inc = "inc2";
if (($first_build eq "false") || ($first_build eq "true" && $first_base eq "true")) {
	#Check if the tool doesn't die when there are only closed open items, because it will check for open work items
	#and the polarion export will throw a Java Exception
 	lives_ok{ITS->accessITS($its_path, $project_name, $rel_inc,$type,$base_open_wi_allw,$buildReport)}, 'Verifying that the tool lives when there are only closed work items';
 	#Get the only revision in the inc2 that is the 10453
 	
	my $rev_list_ref = BuildTool->getRevisions($type); 
	my @rev_list = @$rev_list_ref;	
	#Get the only changed file for the revision 10453 that is the dummy_file.txt
    $build_file_rev_changes_ref = BuildTool->getFileList($rev_list[0],"http://cad-test/repo/crdm/device/firmware/tools/build_tool"); 
    %build_file_rev_changes = %$build_file_rev_changes_ref;
	is(@{$build_file_rev_changes{"http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/dummy_file.txt"}}[0],"10453",'Verifying the build_file_rev_changes');
	
	if($first_base eq "true"){
		%build_file_rev = %$build_file_rev_changes_ref;
		$build_file_rev_ref = \%build_file_rev;
	} 
	
	#Compare list of files from polarion and from the buildInfo
	$build_file_rev_ref = BuildTool->compRevLists($build_file_rev_ref,$build_file_rev_changes_ref);
	#Verifying that the build_file_rev_changes and the build_file are the same
	is(@{$build_file_rev{"http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/dummy_file.txt"}}[0],"10453", "Verifying the build_file_rev");
}

	#Checkout files	
	BuildTool->checkOut($build_file_rev_ref,$vcs_path,$first_build,$first_cron);
	#Verifying if it's exporting the specified file when it's the first build and baseline
	is(-e "trunk/quals/main_fw/code/dummy_file.txt","1", "Verifying that the file \"dummy_file.txt\" exists");
	
	#Creating the build folder inside the code folder
	mkdir("trunk/quals/main_fw/code/build");
	#Create the buildInfo file
	BuildTool->genBuildInfo("trunk/quals/main_fw","trunk",$first_build,$first_cron,"buildInfo.txt",$vcs_path);
	open(FILE,"<trunk/quals/main_fw/code/build/buildInfo.txt");
	my @lines = <FILE>;
	close FILE;
	#Verifying if the buildInfo.txt file/revision is equal to expected
	is($lines[0], "http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/dummy_file.txt \n","Verifying buildInfo files");
	is($lines[1], "10453 ", "Verifying buildInfo revisions");
	system("rm -rf trunk");
	system("rm -rf buildReport");
};

subtest 'Test Case 2.5' => sub {
	plan tests => 1;
####################################################################################
#	
# Verifying that release_history.txt was successfully created for the first build
#
####################################################################################
	#Initializing variables
	my $first_build = "true";
	my $release_history_location = "tags/release_history.txt";
	Build->updateRelHist( $first_build,$release_history_location,"cron","1.1");
	#Read the release_history.txt to check if the build tag is correct
	open(CMD,$release_history_location);
	my $cron_number = <CMD>; 
	chomp($cron_number);
	close CMD;
	is($cron_number,"cron1.1","Verifying build tag in release_history.txt");
	system("rm -rf tags");

};


 